<template>
  <el-row :gutter="20">
    <el-col :span="18"
      ><el-card v-loading="loading">
        <span class="homePage" v-show="id != userInfo.userId">{{
          userInfo.userName + '的个人主页'
        }}</span>
        <div
          class="community-box"
          v-for="item in postList"
          :key="item.postId + '123'"
        >
          <div class="community-content">
            <div class="box-userMessage" v-show="id == userInfo.userId">
              <el-avatar
                :size="32"
                :src="item.userName === proxy.$storage.getItem('userInfo').userName ? 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' : 'https://source.unsplash.com/random'"
              ></el-avatar>
              <span>{{ item.userName }}</span>
              <span>{{ item.createTime }}</span>
              <span v-if="item.status === 3"
                ><el-tag type="danger">该帖子已被管理员删除</el-tag></span
              >
              <el-icon @click="deletePost(item.postId)"><Delete /></el-icon>
            </div>
            <div
              class="box-title"
              @click="toCommunityDetail(item.postId, item.status)"
            >
              {{ item.title }}
            </div>
            <div
              class="box-message"
              @click="toCommunityDetail(item.postId, item.status)"
            >
              {{ postContent(item.content) }}
            </div>
            <div
              class="box-img"
              @click="toCommunityDetail(item.postId, item.status)"
            >
              <img :src="randomUrl + '/' + item.postId" alt="" />
            </div>
          </div>
        </div>
        <el-pagination
          v-show="postList.length !== 0"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[1, 3, 5, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
    </el-col>
    <el-col :span="6">
      <el-card class="chicken-box">
        <span>心灵文案</span>
        <p>{{ chicken }}</p>
      </el-card>
      <el-card class="ranking-box">
        <span>学习排行榜</span>
        <el-table :data="rankMessage" style="width: 100%" max-height="400px">
          <el-table-column prop="name" label="用户名" align="center">
          </el-table-column>
          <el-table-column prop="clockNum" label="打卡次数" align="center">
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { useStore } from 'vuex';
import { Base64 } from 'js-base64';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Delete } from '@element-plus/icons-vue'
import { ref, reactive, computed, onMounted, getCurrentInstance, ComponentInternalInstance } from 'vue'

const storeData = useStore();
const { appContext } = getCurrentInstance() as ComponentInternalInstance;
const proxy = appContext.config.globalProperties;

// 随机图片
const randomUrl = ref('https://source.unsplash.com/random');
// 本用户id
const id = ref(proxy.$storage.getItem('userInfo').userId || '');
// 用户信息
const userInfo = reactive({
  userId: null,
  userName: '',
})
// 是否正在加载中
const loading = ref(false);
// 当前所在页
const pageNum = ref(1);
// 每页的帖子数
const pageSize = ref(3);
// 帖子总数
const total = ref(0);
// 帖子信息
interface PostList {
  commentCount: Number
  content: String
  createTime: String
  postId: Number
  score: Number
  status: Number
  title: String
  type: number
  userId: Number
  userName: String
}
const postList = ref<PostList[]>([]);
// 鸡汤
const chicken = ref('');

onMounted(() => {
  loading.value = true;
  getChicken();
  let data = proxy.$route.query;
  userInfo.userId = Base64.decode(data.i);
  userInfo.userName = Base64.decode(data.n);
  getUserPosts();
})

const rankMessage = computed(() => {
  return storeData.state.userSign.rankMessage;
});

// 跳转到社区详情页
function toCommunityDetail(postId: any, status: any) {
  if (status === 3) {
    return ElMessage({
      type: 'error',
      message: '该帖子已被管理员删除，无法查看',
      duration: 1000,
    });
  }
  const p = Base64.encodeURI(postId);
  proxy.$router.push({
    path: `/communityDetail`,
    query: { p },
  });
};
// 查询其他用户帖子信息
function getUserPosts() {
  proxy.$request.discussPost
    .own(
      {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
      },
      userInfo.userId
    )
    .then((res: any) => {
      if (!res.data) {
        return (loading.value = false);
      }
      postList.value = res.data.list;
      total.value = res.data.total;
      loading.value = false;
    });
};
// 把内容的css标签删除
function postContent(content: any) {
  return content.replace(/<[^>]+>/g, '');
};
// 当前页显示帖子个数改变时触发
function handleSizeChange(newPageSize: any) {
  pageSize.value = newPageSize;
  loading.value = true;
  getUserPosts();
};
// 页数改变时触发
function handleCurrentChange(newPageNum: any) {
  pageNum.value = newPageNum;
  loading.value = true;
  getUserPosts();
};
// 删除帖子
function deletePost(postId: any) {
  ElMessageBox.confirm('此操作将删除帖子, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      proxy.$request.discussPost.deletePost(postId).then((res: any) => {
        ElMessage({
          type: 'success',
          message: res.msg,
          duration: 1000,
        });
        getUserPosts();
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消操作',
        duration: 1000,
      });
    });
};
// 获取鸡汤
function getChicken() {
  proxy.$request.chicken.getChicken().then((res: any) => {
    chicken.value = res.data;
  });
};
</script>

<style lang="less" scoped>
.homePage {
  font-size: 20px;
  font-weight: 700;
  display: block;
  margin-bottom: 20px;
}

.community-box {
  position: relative;

  .community-content {
    padding-bottom: 20px;
    margin-bottom: 15px;
    border-bottom: 1px solid #eee;
    text-align: left;

    .box-userMessage {
      height: 40px;
      line-height: 40px;
      margin-bottom: 10px;
      display: block;

      .el-avatar {
        float: left;
        margin: 4px;
      }

      span:nth-child(2) {
        display: inline-block;
        color: #303133;
        font-size: 20px;
      }

      span:nth-child(3) {
        display: inline-block;
        color: #606266;
        font-size: 14px;
        margin: 0 10px;
      }

      span:nth-child(4) {
        display: inline-block;
        margin-left: 120px;
      }

      i {
        display: none;
        float: right;
        font-size: 20px;
        margin: 10px 10px 10px 0;

        &:hover {
          color: #409eff;
        }
      }
    }

    &:hover {
      cursor: default;

      .box-userMessage i {
        display: inline-block;
      }
    }

    .box-title {
      font-weight: 700;
      color: #303133;
      font-size: 20px;
      display: inline-block;
      width: 65%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .box-message {
      min-height: 30px;
      font-size: 14px;
      line-height: 30px;
      color: #555666;
      display: inline-block;
      width: 65%;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      overflow: hidden;

      /deep/ p {
        margin: 0;
      }
    }

    .box-img {
      position: absolute;
      width: 30%;
      height: 140px;
      top: 40px;
      right: 0;
      border-radius: 5px;
      overflow: hidden;
      img{
        height: 100%;
        width: 100%;
        background-size: cover;
      }
    }
  }

  .box-icon {
    height: 40px;
    line-height: 40px;
    margin-bottom: 20px;
    border-bottom: 1px solid #eee;

    .icon-message {
      font-size: 14px;
      color: #999999;
      margin-right: 30px;
      cursor: default;

      span {
        margin-left: 5px;
      }
    }

    .icon-message:hover {
      color: #555666;
    }

    .isLiked {
      color: #409eff;
    }
  }
}

.community-message {
  min-height: 200px;
  margin-bottom: 20px;

  .recommend-box {
    height: 44px;
    margin-bottom: 15px;

    img {
      float: left;
      height: 44px;
      width: 44px;
      margin-right: 10px;
    }

    .recommend-message {
      float: left;
      width: 120px;

      span:nth-child(1) {
        font-size: 14px;
        color: #222226;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      span:nth-child(2) {
        font-size: 12px;
        color: #555666;
      }
    }

    .el-button {
      float: right;
      margin: 8px 0;
    }
  }

  .recommend-button {
    text-align: center;
  }

  .myCommunity-box {
    margin-bottom: 15px;

    img {
      float: left;
      height: 20px;
      width: 20px;
      margin-right: 10px;
    }

    .myCommunity-message {
      font-size: 14px;
    }
  }
}

.chicken-box,
.ranking-box {
  span {
    display: block;
    margin-bottom: 10px;
    font-weight: 600;
  }
}

.chicken-box {
  margin-bottom: 10px;
  height: 180px;

  p {
    font-family: '宋体';
    padding: 0 11px;
    font-size: 16px;
    color: #e6a23c;
  }
}

.ranking-box {
  min-height: 200px;
  max-height: 600px;
  overflow: auto;
}
</style>
